package com.it.water.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;

/**
 * 自定义元数据对象处理器
 *
 * @author HelloWorld505
 * @author <a href="mailto:2038322151@qq.com">Miraitowa_zcx</a>
 */
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

    /**
     * 插入操作，自动填充
     *
     * @param metaObject 公共字段
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());

        metaObject.setValue("createtime", new Timestamp(new Date().getTime()));
        metaObject.setValue("updatetime", new Timestamp(new Date().getTime()));
    }

    /**
     * 更新操作，自动填充
     *
     * @param metaObject 公共字段
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]...");
        log.info(metaObject.toString());

/*     long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);*
*/
        metaObject.setValue("updatetime", new Timestamp(new Date().getTime()));
    }
}
